﻿using DataAccess;
using KhoaLuan.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace KhoaLuan.MainSys.Manager.Controls
{
    public partial class ctAddNewArticle : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    var userid = UserLogin.GetUserID;

                    var proxypostcat = new tbPostsCat_Proxy();
                    var _articleid = Request.QueryString["ArticleID"];
                    var org = UserLogin.GetCurrentOrg;
                    if (org == null)
                        throw new Exception();
                    lbOrg.InnerHtml = org.NameOrg;

                    if (_articleid != null)
                    {
                        if(!SecurityServices.HasPermison(this.AppRelativeVirtualPath,userid, TypeAudit.Edit))
                            Response.Redirect("~/Mainsys/Manager/AccessDeny.aspx");
                        var proxypost = new tbPosts_Proxy();
                        var post = proxypost.GetById_tbPosts_Proxy(int.Parse(_articleid)).ToObject<tbPosts>();
                        if (post.OrgID != org.OrgID)
                        {
                            //mo 1 bai viet khong thuoc org da dang nhap
                            throw new Exception();
                        }

                        btnDangBai.Value = "Lưu";
                        if (post.ImagePath != null)
                            imgHinhMinhHoa.ImageUrl = post.ImagePath;
                        if (post != null)
                        {
                            if (post.TypePosts == 2)
                            {
                                typeThongBao.Checked = true;
                                cbChuyenMuc.Enabled = false;
                            }
                            else
                                typeTinTuc.Checked = true;

                            cbChuyenMuc.DataSource = proxypostcat.GetByOrgID_tbPostsCat_Proxy(org.OrgID);
                            cbChuyenMuc.DataTextField = "PostsCatName";
                            cbChuyenMuc.DataValueField = "PostsCatID";
                            if (post.PostsCatID != null)
                                cbChuyenMuc.SelectedValue = post.PostsCatID.ToString();
                            cbChuyenMuc.DataBind();
                            txtTieuDe.Value = post.Title;
                            txtNoiDung.Text = HttpUtility.HtmlDecode(post.Content);
                            txtVanTat.Text = HttpUtility.HtmlDecode(post.Intro);
                        }
                        else
                        {
                            //khong ton tai bai viet
                            throw new Exception();
                        }
                    }
                    else
                    {
                        if (!SecurityServices.HasPermison(this.AppRelativeVirtualPath, userid, TypeAudit.AddNew))
                            Response.Redirect("~/Mainsys/Manager/AccessDeny.aspx");
                        typeTinTuc.Checked = true;
                        cbChuyenMuc.DataSource = proxypostcat.GetByOrgID_tbPostsCat_Proxy(org.OrgID);
                        cbChuyenMuc.DataTextField = "PostsCatName";
                        cbChuyenMuc.DataValueField = "PostsCatID";
                        cbChuyenMuc.DataBind();
                    }
                }
                catch (Exception)
                {

                }

            }

            //            txtNoiDung.config.toolbar = new object[]
            //            {                
            //                new object[] { "Source", "-", "Save", "NewPage", "Preview", "-", "Templates" },
            //    new object[] { "Cut", "Copy", "Paste", "PasteText", "PasteFromWord", "-", "Print", "SpellChecker", "Scayt" },
            //    new object[] { "Undo", "Redo", "-", "Find", "Replace", "-", "SelectAll", "RemoveFormat" },
            //    new object[] { "Form", "Checkbox", "Radio", "TextField", "Textarea", "Select", "Button", "ImageButton", "HiddenField" },
            //    "/",
            //    new object[] { "Bold", "Italic", "Underline", "Strike", "-", "Subscript", "Superscript" },
            //    new object[] { "NumberedList", "BulletedList", "-", "Outdent", "Indent", "Blockquote", "CreateDiv" },
            //    new object[] { "JustifyLeft", "JustifyCenter", "JustifyRight", "JustifyBlock" },
            //    new object[] { "BidiLtr", "BidiRtl" },
            //    new object[] { "Link", "Unlink", "Anchor" },
            //    new object[] { "Image", "Flash", "Table", "HorizontalRule", "Smiley", "SpecialChar", "PageBreak", "Iframe" },
            //    "/",
            //    new object[] { "Styles", "Format", "Font", "FontSize" },
            //    new object[] { "TextColor", "BGColor" },
            //    new object[] { "Maximize", "ShowBlocks", "-", "About" }
            //};

            //CKEditor1.CKEditorInstanceEventHandler = new System.Collections.Generic.List<object>();
            //CKEditor1.CKEditorInstanceEventHandler.Add(new object[] { "instanceReady", "function (evt) { alert('Event Handler attached on CKEditorInstanceEventHandler to editor: ' + evt.editor.name);}" });
            //            CKEditor1.CKEditorInstanceEventHandler.Add(new object[] { "instanceReady"});
            //, "function (evt) { alert('Event Handler attached on CKEditorInstanceEventHandler to editor: ' + evt.editor.name);}" });

        }
    }
}